package cn.lopr.im.android.mvp.model;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;

import cn.lopr.im.android.bean.CallbackResult;
import cn.lopr.im.android.common.Constant;
import cn.lopr.im.android.common.OnCallBackListener;
import cn.lopr.im.android.mvp.contact.IInfoContact;
import cn.lopr.im.android.utils.Apis;
import cn.lopr.im.android.utils.HttpUtils;

/**
 * @author shuangyang7
 * @date 2020/12/19 11:51
 */
public class InfoModel implements IInfoContact.IInfoModel {

    @Override
    public void update(String name, String birth, String introduction, String sex, OnCallBackListener<String, String> onCallBackListener) {
        String param = String.format("?name=%s&birth=%s&introduction=%s&sex=%s", name, birth, introduction, sex);
        // ip:port/url param
        // http://  localhost  : 8080  /im/user/login?phone=18255431881&password=000
        HttpUtils.doGetForResult(Apis.WebApis.INFO_UPDATE.getValue(), param, data -> {

            Log.d("json", data);

            if (TextUtils.isEmpty(data)) {
                onCallBackListener.failure(Constant.CALLBACK_ERROR_MESSAGE);
                return;
            }

            Type jsonType = new TypeToken<CallbackResult<String>>() {
            }.getType();
            CallbackResult<String> callbackResult = new Gson().fromJson(data, jsonType);

            if (callbackResult.getStatus() == Constant.ResponseCode.SUCCESS.getCode()) {
                onCallBackListener.success(callbackResult.getMsg());

            } else {
                onCallBackListener.failure(callbackResult.getMsg());

            }
        });
    }
}
